﻿using System.IO;
using System.Windows.Forms;
using System.Windows.Input;
using IS_Ever_Green.DataAccess.Model;
using IS_Ever_Green.DataAccess.Model.Repositories;

namespace IS_Ever_Green.ViewModel
{
    public class ProfileViewModel : BaseViewModel
    {
        private IUserRepository _userRepository;
        private UserModel _currentUser;
        private string _message;

        public UserModel CurrentUser
        {
            get { return _currentUser; }
            set
            {
                _currentUser = value;
                OnPropertyChanged(nameof(CurrentUser));
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                OnPropertyChanged(nameof(Message));
            }
        }

        public ICommand SaveUserProfileViewCommand { get; }
        public ICommand AddPhotoUserViewCommand { get; }

        public ProfileViewModel()
        {
            _userRepository = new UserRepository();
            SaveUserProfileViewCommand = new CommandViewModel(ExecuteSaveUserProfileViewCommand);
            AddPhotoUserViewCommand = new CommandViewModel(ExecuteAddPhotoUserViewCommand);
        }

        private async void ExecuteSaveUserProfileViewCommand(object obj)
        {
            _userRepository.SaveUserProfile(CurrentUser, CurrentUser.PhotoUser);

            Message = "Данные сохранены";

            await Task.Delay(5000);
            Message = "";
        }
        private void ExecuteAddPhotoUserViewCommand(object obj)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFileName = openFileDialog.FileName;
                byte[] photoBytes = File.ReadAllBytes(selectedFileName);                

                CurrentUser.PhotoUser = photoBytes;
            }

            OnPropertyChanged(nameof(CurrentUser));

        }
    }
}
